//
//  RW_MixedView.m
//  SleepApp
//
//  Created by MAC on 2021/3/22.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_MixedView.h"
#import "RW_MixedAudioView.h"

@interface RW_MixedView ()
/** 主列表展示视图 */
@property (nonatomic,strong) UIScrollView *scroll_view;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *content_view;

@end
@implementation RW_MixedView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.scroll_view];
        [self.scroll_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
    }
    return self;
}
#pragma mark -- 初始化数据操作
-(void)setWlclass_view_model:(RW_MixedViewModel *)wlclass_view_model{
    _wlclass_view_model = wlclass_view_model;
    NSLog(@"%@",self.scroll_view);
    [self setAudioModelList:_wlclass_view_model.mixed_audio_view_array];
}
-(void)setAudioModelList:(NSMutableArray<RW_MixedAudioView *> *)viewList{
    [self.content_view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [viewList each:^(RW_MixedAudioView *obj, NSInteger idx) {
        obj.roowViewController = self.roowViewController;
        [self.content_view addSubview:obj];
        
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.content_view);
            make.height.mas_offset(100);
            
            if (idx == 0){//第一个
                make.top.mas_equalTo(self.content_view);
            } else {//其他
                RW_MixedAudioView *topView = [self.wlclass_view_model.mixed_audio_view_array objectAtIndex:(idx - 1)];
                make.top.mas_equalTo(topView.mas_bottom);
                if (idx + 1 == viewList.count){//最后一个
                    make.bottom.mas_equalTo(self.content_view);
                }
            }
        }];
    }];
}


#pragma mark -- 懒加载
-(UIScrollView *)scroll_view{
    if (!_scroll_view){
        _scroll_view = [[UIScrollView alloc]init];
        
        [_scroll_view addSubview:self.content_view];
        [self.content_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_scroll_view);
            make.width.equalTo(_scroll_view);
        }];
    }
    return _scroll_view;
}
// 约束参照视图
- (UIView *)content_view {
    if (_content_view == nil) {
        _content_view = [[UIView alloc] init];
        _content_view.backgroundColor = [UIColor yellowColor];
    }
    return _content_view;
}




/** 释放当前播放器属性 */
-(void)releaseVM{
    [_content_view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
        obj = nil;
    }];
    [_wlclass_view_model.mixed_audio_view_array enumerateObjectsUsingBlock:^(RW_MixedAudioView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj releaseVM];
        obj = nil;
    }];
    _content_view = nil;
    _scroll_view = nil;
}
@end
